package com.junci.mtest.utils;

import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;


import com.junci.mtest.model.bean.DefaultBean;

import java.util.ArrayList;
import java.util.List;

public class MyDiffUtilCallback extends DiffUtil.Callback {

    private List<DefaultBean> oldCoderList = new ArrayList<>();
    private List<DefaultBean> newCoderList = new ArrayList<>();

    // 通过构造传入新旧数据集
    public MyDiffUtilCallback(List<DefaultBean> oldCoderList, List<DefaultBean> newCoderList) {
        this.oldCoderList = oldCoderList;
        this.newCoderList = newCoderList;
    }

    @Override
    public int getOldListSize() {
        return oldCoderList == null ? 0 : oldCoderList.size();
    }

    @Override
    public int getNewListSize() {
        return newCoderList == null ? 0 : newCoderList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        DefaultBean oldCoderBean = oldCoderList.get(oldItemPosition);
        DefaultBean newCoderBean = oldCoderList.get(newItemPosition);
        if (oldCoderBean != null && newCoderBean != null){
            int oldId = oldCoderList.get(oldItemPosition).getId();
            int newId = newCoderList.get(newItemPosition).getId();
            if (oldId == newId){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        String oldName = oldCoderList.get(oldItemPosition).getName();
        String newName = newCoderList.get(newItemPosition).getName();
        if (TextUtils.isEmpty(oldName) || TextUtils.isEmpty(newName)){
            return false;
        }
        if (oldName.equals(newName)){
            return true;
        }
        return false;
    }

    @Nullable
    @Override
    public Object getChangePayload(int oldItemPosition, int newItemPosition) {
//        System.out.println(":> getChangePayload +++ old: " + oldItemPosition
//        + ", +++ new: " + newItemPosition);
        return super.getChangePayload(oldItemPosition, newItemPosition);
    }
}

